package com.ronri.ams.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.ams.bean.entitybean;
import com.ronri.common.UrlEncode;
import com.ronri.db.RemoteDataService;

import java.util.HashMap;
import java.util.Map;

import static com.ronri.ams.SystemEnvironmental.Sample_INPUT_AREA;

/**
 * Created by Administrator on 2017/10/26.
 */

public class AddEntityActivity extends abstract_activity {

    private EditText et_name;//生产主体名称
    private EditText et_address;//生产主体地址
    private TextView tv_regionCode;//区域选择

    private String region_code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_entity);

        // 设置标题
        setTitle("新增受检对象", "确定");

        // 初始化
        init();

        et_name = findViewById(R.id.et_name);
        et_address = findViewById(R.id.et_address);
        tv_regionCode = findViewById(R.id.tv_regionCode);

        //区域选择
        tv_regionCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), activity_region_select.class);
                intent.putExtra("add_entity", true);
                startActivityForResult(intent, Sample_INPUT_AREA);
            }
        });
    }

    @Override
    public boolean SureReturn() {
        // 新增主体信息,先进行判空
        if (et_name.getText().toString().equals("") || et_address.getText().toString().equals("")) {
            Toast.makeText(this, "请输入所有字段", Toast.LENGTH_SHORT).show();
        } else if (region_code.equals("")) {
            Toast.makeText(this, "请选择所在区域", Toast.LENGTH_SHORT).show();
        } else {
            RemoteDataService remoteDataService = new RemoteDataService();

            String szRemoteAddress = "http://120.55.93.238:8080/ams/api/saveEnt.json";
            Map<String, Object> mParam = new HashMap<String, Object>();

            mParam.put("addFrom", 1);
            mParam.put("addMark", "android");
            mParam.put("region", region_code);
            mParam.put("name", et_name.getText().toString());
            mParam.put("address", et_address.getText().toString());

            remoteDataService.getRemoteData(szRemoteAddress,
                    UrlEncode.getUrlParamsByMap(mParam, false),
                    InitApplication.SystemConfig.getModule().getLogin().getMethod(),
                    new RemoteDataService.OnRemoteDataEvent() {

                        @Override
                        public void OnResultEvent(String szString) {
                            if (szString.contains("网络连接失败")) {
                                Toast.makeText(AddEntityActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            entitybean entitybean = JSON.parseObject(szString, entitybean.class);

                            if (entitybean.isSuccess()) {
                                Toast.makeText(AddEntityActivity.this, "添加成功", Toast.LENGTH_SHORT).show();

                                Intent intent = new Intent();
                                intent.putExtra("name", et_name.getText().toString());
                                intent.putExtra("id", Integer.parseInt(entitybean.getData().getEnt_id()));

                                setResult(RESULT_OK, intent);
                                finish();
                            } else {
                                Toast.makeText(AddEntityActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void OnRuningState(int nStatus) {

                        }

                        @Override
                        public String OnGetCustomParam() {
                            return null;
                        }
                    });

        }

        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Sample_INPUT_AREA: {
                    Bundle bundle = data.getExtras();
                    region_code = bundle.getString("region_code");
                    String region_name = bundle.getString("region_name");
                    tv_regionCode.setText(region_name);
                }
            }
        }
    }
}
